package cn.suxiaoxiang.suwebverify.api;

import cn.suxiaoxiang.suwebverify.SuWebVerify;
import org.json.simple.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static cn.suxiaoxiang.suwebverify.SuWebVerify.main;

/**
 * EncryptionKey - 获取加密密钥API
 *
 * @author 程序员-Su_xiaoxiang
 * @date 2025/4/7
 */
@WebServlet("/api/encryption-key")
public class EncryptionKey extends HttpServlet {
    private final SuWebVerify thisplugin;

    public EncryptionKey(SuWebVerify plugin) {
        this.thisplugin = plugin;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        resp.setHeader("Access-Control-Allow-Headers", "Content-Type");

        // 从配置文件获取加密密钥
        String key = main.getConfig().getString("encryption.key", "your-secret-key-123");

        JSONObject responseJson = new JSONObject();
        responseJson.put("key", key);
        resp.getWriter().write(responseJson.toJSONString());
    }
} 